<?php
class Rating extends AppModel {
	var $name = 'Rating';
	
	function checkRating($userId, $productId)
	{
		$sql="SELECT COUNT(`accurate_description`) AS Num_accurate, 
				COUNT(`communication`) AS Num_communication, 
				COUNT(`shipping_speed`) AS Num_shipping_speed, 
				COUNT(`recommend_seller`) AS Num_recommend_seller, 
				AVG(`accurate_description`) AS Desc_accurate, 
				AVG(`communication`) AS Desc_communication, 
				AVG(`shipping_speed`) AS Desc_shipping_speed, 
				AVG(`recommend_seller`) AS Desc_recommend_seller 
				FROM `authentic_ratings` 
				WHERE
				`seller_userid`='$userId' AND
				`product_id`='$productId'
				";
		$arr = $this->query($sql);
		return $this->starArray($arr); 
	}

        function sadmincheckRating($userId)
	{
		$sql="SELECT COUNT(`accurate_description`) AS Num_accurate,
				COUNT(`communication`) AS Num_communication,
				COUNT(`shipping_speed`) AS Num_shipping_speed,
				COUNT(`recommend_seller`) AS Num_recommend_seller,
				AVG(`accurate_description`) AS Desc_accurate,
				AVG(`communication`) AS Desc_communication,
				AVG(`shipping_speed`) AS Desc_shipping_speed,
				AVG(`recommend_seller`) AS Desc_recommend_seller
				FROM `authentic_ratings`
				WHERE
				`seller_userid`='$userId'";
		$arr = $this->query($sql);
		return $this->starArray($arr);
	}
	
	function starArray($arr, $starArr=array())
	{
		if ($arr[0][0]['Num_accurate'] > 15)
		{
			$starArr['Desc_accurate']['value']=round($arr[0][0]['Desc_accurate'], 0);
			$starArr['Desc_accurate']['class']=$this->starImageName($starArr['Desc_accurate']['value']);
		}
		if ($arr[0][0]['Num_communication'] > 15)
		{
			$starArr['Desc_communication']['value']=round($arr[0][0]['Desc_communication'], 0);
			$starArr['Desc_communication']['class']=$this->starImageName($starArr['Desc_communication']['value']);
		}
		if ($arr[0][0]['Num_shipping_speed'] > 15)
		{
			$starArr['Desc_shipping_speed']['value'] = round($arr[0][0]['Desc_shipping_speed'], 0);
			$starArr['Desc_shipping_speed']['class']=$this->starImageName($starArr['Desc_shipping_speed']['value']);
		}
		if ($arr[0][0]['Num_recommend_seller'] > 15)
		{
			$starArr['Desc_recommend_seller']['value'] = round($arr[0][0]['Desc_recommend_seller'], 0);
			$starArr['Desc_recommend_seller']['class']=$this->starImageName($starArr['Desc_recommend_seller']['value']);
		}
			
		return $starArr;
	}
	
	function starImageName($value)
	{
		switch ($value)
		{
			case ($value < 3):
				return 'star-sign';
				break;
			case ($value == 3):
				return 'yellow-star-sign';
				break;
			case ($value > 3):
				return 'green';
				break;
			default:
				return 'white';
				break;
		}
	}
}